﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Game.Models
{
    public class Game
    {
        private List<Player> players;
        private string id;
        private Map map;

        public Map Map
        {
            get { return map; }
        }

        public string Id
        {
            get
            {
                return id;
            }
        }
        
        public List<Player> Players
        {
            get
            {
                return players;
            }
        }

        public Game(string id)
        {
            players = new List<Player>();
            if (id != null)
            {
                this.id = GenerateID();
            }
            else
            {
                this.id = id;
            }
        }

        public void SaveGame(string id)
        {
            try
            {
                string games = "";
                using (StreamReader reader = new StreamReader("game.gm"))
                {
                    games = reader.ReadToEnd();
                }
                using (StreamWriter writer = new StreamWriter("game.gm"))
                {
                    if (games.Contains(id))
                    {
                        string oldGame = "";
                        foreach (var item in games.Split('\n'))
                        {
                            if (item.Contains(id))
                            {
                                oldGame = item;
                            }
                        }
                        games = games.Replace(oldGame, this.ToString());
                    }
                    else
                    {
                        games += this.ToString() + "\n";
                    }
                    writer.Write(games);
                }
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Server files missing");
            }
        }

        public string GenerateID()
        {
            return Guid.NewGuid().ToString("N");
        }

        public void AddPlayer(Player player)
        {
            if (players.Count == 1 && players[0].Name == "No players")
            {
                players[0] = player;
            }
            else if (players.Count < 10)
            {
                this.players.Add(player);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(Id + ";");
            if (players.Count > 0)
            {
                foreach (Player player in players)
                {
                    builder.Append(player.ToString() + ",");
                }
                builder.Remove(builder.Length - 1, 1);
            }
            else
            {
                builder.Append("No players");
            }
            return builder.ToString();
        }
    }
}